// /**
//  * 部门管理初始化
//  */
// var PaymentTotal = {
//     id: "PaymentTotalTable",	//表格id
//     seItem: null,		//选中的条目
//     table: null,
//     layerIndex: -1
// };
//
// /**
//  * 初始化表格的列
//  */
// PaymentTotal.initColumn = function () {
//     var columns = [
//         {field: 'selectItem', radio: true},
//
//         {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle', width: '50px'},
//         {title: 'customerId', field: 'customerId', visible: false, align: 'center', valign: 'middle', width: '50px'},
//         {
//             title: 'reservationsId',
//             field: 'reservationsId',
//             visible: false,
//             align: 'center',
//             valign: 'middle',
//             width: '50px'
//         },
//         {title: '客户姓名', field: 'customerName', align: 'center', valign: 'middle', sortable: true,},
//         {title: '客户电话', field: 'customerPhone', align: 'center', valign: 'middle', sortable: true,},
//         {
//             title: '客户性别', field: 'customerSex', align: 'center', valign: 'middle', sortable: true,
//             formatter: function (value, row, index) {
//                 if (value == 1) {
//                     return "男";
//                 } else if (value == 2) {
//                     return "女"
//                 } else if (value == 0) {
//                     return ""
//                 } else {
//                     return ""
//                 }
//             }
//         },
//         {title: '客户年龄', field: 'customerAge', align: 'center', valign: 'middle', sortable: true,},
//         {title: '客户状态', field: 'customerStatus', align: 'center', valign: 'middle', sortable: true,},
//         {title: 'TC', field: 'fieldTc', align: 'center', valign: 'middle', sortable: true,},
//         {title: '医生', field: 'doctor', align: 'center', valign: 'middle', sortable: true,},
//         // {title: '化名', field: 'pseudonym', align: 'center', valign: 'middle', sortable: true,},
//         {title: '现金收费', field: '现金收费', align: 'center', valign: 'middle', sortable: true,},
//         {title: '扫码支付', field: '扫码支付', align: 'center', valign: 'middle', sortable: true,},
//         {title: '刷卡', field: '刷卡', align: 'center', valign: 'middle', sortable: true,},
//         {title: '美团', field: '美团', align: 'center', valign: 'middle', sortable: true,},
//         {title: '积分抵扣', field: '积分抵扣', align: 'center', valign: 'middle', sortable: true,},
//         {title: '华容时代', field: '华容时代', align: 'center', valign: 'middle', sortable: true,},
//         {title: '优惠券', field: '优惠券', align: 'center', valign: 'middle', sortable: true,},
//         {title: '医保', field: '医保', align: 'center', valign: 'middle', sortable: true,},
//         {title: '折扣', field: '折扣', align: 'center', valign: 'middle', sortable: true,},
//         {
//             title: '结账时间', field: 'rechargeTime', align: 'center', valign: 'middle', sortable: true, width: '12%',
//             formatter: function (value, row, index) {
//                 return value.substring(0, 19);
//             }
//         },
//         {title: '备注', field: 'remarks', align: 'center', valign: 'middle', sortable: true},
//     ]
//     return columns;
// };
//
// PaymentTotal.close = function () {
//     // parent.layer.close(window.parent.Customer.layerIndex);
//     var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
//     parent.layer.close(index);
// }
//
//
// /**
//  * 检查是否选中
//  */
// PaymentTotal.check = function () {
//     var selected = $('#' + this.id).bootstrapTable('getSelections');
//     if (selected.length == 0) {
//         Feng.info("请先选中表格中的某一记录！");
//         return false;
//     } else {
//         Recharge.seItem = selected[0];
//         return true;
//     }
// };
//
// /**
//  * 查询预约客户列表
//  */
// PaymentTotal.search = function () {
//     PaymentTotal.table.refresh({query: PaymentTotal.formParams()});
//     $.ajax({
//         type: "GET",
//         url: Feng.ctxPath + '/paymentTotal/sumBorrowAmount',
//         data: PaymentTotal.formParams(),
//         dataType: "json",
//         success: function (data) {
//             $('#sumMoney').html(data.borrowAmount_sum);
//         }
//     });
// };
//
// PaymentTotal.resetSearch = function () {
//     $("#customerName").val("");
//     $("#customerPhone").val("");
//     $("#startTime").val("");
//     $("#endTime").val("");
//     $("#fieldTc").val("");
//     $("#doctor").val("");
//     $("#customerStatus").val("");
//     document.getElementById("startTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "00:00:00";
//     document.getElementById("endTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "23:59:59";
//     PaymentTotal.search();
// };
//
// /**
//  * 查询表单提交参数对象
//  * @returns {{}}
//  */
// PaymentTotal.formParams = function () {
//     var queryData = {};
//     queryData['customerName'] = $("#customerName").val();
//     queryData['customerPhone'] = $("#customerPhone").val();
//     queryData['startTime'] = $("#startTime").val();
//     queryData['endTime'] = $("#endTime").val();
//     queryData['fieldTc'] = $("#fieldTc").val();
//     queryData['doctor'] = $("#doctor").val();
//     queryData['customerStatus'] = $("#customerStatus").val();
//     return queryData;
// }
// var myDate = new Date();
// var month;
// var day;
// if ((myDate.getMonth() + 1) < 10) {
//     month = "0" + (myDate.getMonth() + 1);
// } else {
//     month = "" + (myDate.getMonth() + 1);
// }
// if (myDate.getDate() < 10) {
//     day = "0" + myDate.getDate();
// } else {
//     day = "" + myDate.getDate();
// }
// $(function () {
//     $.post(Feng.ctxPath + "/tc/getList", function (data) {
//         var txt = '<option value="">--请选择TC--</option>';
//         for (var i = 0; i < data.length; i++) {
//             txt += '<option value="' + data[i].tcName + '">' + data[i].tcName + '</option>';
//         }
//         document.getElementById('fieldTc').innerHTML = txt;
//         //状态单选
//         var seletefieldTc = $('#fieldTcValue').val();
//         if (seletefieldTc != "") {
//             $("#fieldTc option[value=" + seletefieldTc + "]").attr("selected", "selected");
//         }
//     }, "json");
//     document.getElementById("startTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "00:00:00";
//     document.getElementById("endTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "23:59:59";
//     var defaultColunms = PaymentTotal.initColumn();
//     var table = new BSTable(PaymentTotal.id, "/paymentTotal/getReport", defaultColunms);
//     table.setPaginationType("server");
//     table.setQueryParams(PaymentTotal.formParams());
//     PaymentTotal.table = table.init();
//     PaymentTotal.search();
// });
//***表格***
var data_report = null;
var myDate = new Date();
var month;
var day;
var days=myDate.getDate()-1;
if ((myDate.getMonth() + 1) < 10) {
    month = "0" + (myDate.getMonth() + 1);
} else {
    month = "" + (myDate.getMonth() + 1);
}
if (myDate.getDate() < 10) {
    day = "0" + myDate.getDate();
} else {
    day = "" + myDate.getDate();
}
if (days < 10) {
    days = "0" + days;
} else {
    days = "" + days;
}
document.getElementById("startTime").value = myDate.getFullYear() + "-" + month + "-" + days + " " + "16:00:00";
document.getElementById("endTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "16:00:00";

function downLoad() {
    var queryData = {};
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();
    var param = parseParam(queryData);
    location.href = Feng.ctxPath + '/paymentTotal/paymentExport?' + param;
}

function search_jztj() {
    var customerName = $('#customerName').val();
    var customerPhone = $('#customerPhone').val();
    var startTime = $('#startTime').val();
    var endTime = $('#endTime').val();
    var fieldTc = $('#fieldTc').val();
    var customerStatus = $('#customerStatus').val();
    var reach = $('#reach').val();
    $.ajax({
        type: "post",
        url: Feng.ctxPath + "/paymentTotal/getReport",
        dataType: "json",
        data: {
            'customerName': customerName,
            'customerPhone': customerPhone,
            'startTime': startTime,
            'endTime': endTime,
            'fieldTc': fieldTc,
            'reach': reach,
            'customerStatus': customerStatus
        },
        success: function (data) {
            var arr = [];
            data.forEach(function (v1) {
                var obj = {};
                obj['客户姓名'] = v1['客户姓名'];
                obj['电话'] = v1['电话'];
                obj['性别'] = v1['性别'];
                obj['年龄'] = v1['年龄'];
                obj['时间'] = v1['时间'];
                obj['收费人'] = v1['收费人'];
                obj['客户状态'] = v1['客户状态'];
                obj['TC'] = v1['TC'];
                obj['现金'] = v1['现金'];
                obj['扫码支付'] = v1['扫码支付'];
                obj['银行卡'] = v1['银行卡'];
                obj['医保'] = v1['医保'];
                obj['分期贷款'] = v1['分期贷款'];
                obj['美团'] = v1['美团'];
                obj['华荣时代'] = v1['华荣时代'];
                obj['糯米'] = v1['糯米'];
                obj['积分抵扣'] = v1['积分抵扣'];
                obj['代金劵'] = v1['代金劵'];
                obj['VIP收费'] = v1['VIP收费'];
                obj['百度钱包'] = v1['百度钱包'];
                obj['社保卡'] = v1['社保卡'];
                obj['欠费'] = v1['欠费'];
                obj['分期收费'] = v1['分期收费'];

                // obj['医生'] = v1['医生'];
                Object.keys(v1).forEach(function (v2) {
                    if (v2 !== '客户姓名' && v2 !== '电话' && v2 !== '性别' && v2 !== '年龄' && v2 !== '时间' && v2 !== '收费人' && v2 !== '客户状态' && v2 !== 'TC' && v2 !== '备注' && v2 != 'customerId'
                        && v2 !== '现金' && v2 !== '扫码支付' && v2 !== '银行卡' && v2 !== '医保' && v2 !== '分期贷款'
                        && v2 !== '美团' && v2 !== '华荣时代' && v2 !== '糯米' && v2 !== '积分抵扣' && v2 !== '代金劵'
                        && v2 !== 'VIP收费' && v2 !== '百度钱包' && v2 !== '社保卡' && v2 !== '欠费' && v2 !== '分期收费') {
                        obj[v2] = v1[v2]
                    }
                });
                obj['备注'] = v1['备注'];
                arr.push(obj);
            });
            data_report = arr;
            renders();
        },
        error: function (msg) {
            alert(msg);
        }
    });
}

//重置
function resetSearch_jztj() {
    document.getElementById("startTime").value = myDate.getFullYear() + "-" + month + "-" + days + " " + "16:00:00";
    document.getElementById("endTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "16:00:00";
    var fieldTc = $('#fieldTc').val('');
    var customerStatus = $('#customerStatus').val('');
}

function isRealNum(val) {
    // isNaN()函数 把空串 空格 以及NUll 按照0来处理 所以先去除
    if (val === "" || val == null) {
        return false;
    }
    if (!isNaN(val)) {
        return true;
    } else {
        return false;
    }
}

/**
 * 导出
 * @returns {{}}
 */

function downLoad() {
    var queryData = {};
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();
    var param = parseParam(queryData);
    location.href = Feng.ctxPath + '/paymentTotal/paymentExport?' + param;
}

var parseParam = function (param, key) {
    var paramStr = "";
    if (param instanceof String || param instanceof Number || param instanceof Boolean) {
        paramStr += "&" + key + "=" + param;
    } else {
        $.each(param, function (i) {
            var k = key == null ? i : key + (param instanceof Array ? "[" + i + "]" : "." + i);
            paramStr += '&' + parseParam(this, k);
        });
    }
    return paramStr.substr(1);
};


//请求到数据渲染到页面
function renders() {
    var report_table = $("#report_table")[0];
    var table_th = document.createElement("tr");
    var str_th = '';
    var ofragment = document.createDocumentFragment();
    //刷新表
    $("#report_table").empty();
    //添加表头
    table_th.class = 'table-bordered';
    Object.keys(data_report[0]).forEach(function (v) {
        str_th += "<th style='text-align: center;height: 36px;'>" + v + "</th>";
    });
    table_th.innerHTML = str_th;
    report_table.appendChild(table_th);
    //添加表内容

    // data_report.forEach(function (v1) {
    //     var str_td = '';
    //     var tr = document.createElement("tr");
    //     Object.keys(v1).forEach(function (v2) {
    //         str_td += "<td>" + v1[v2] + "</td>";
    //     });
    //     tr.innerHTML = str_td;
    //     ofragment.appendChild(tr);
    // });
    for (var i = 0; i < data_report.length - 2; i++) {
        var str_td = '';
        var tr = document.createElement("tr");
        Object.keys(data_report[i]).forEach(function (v2) {
            str_td += "<td style='height: 36px;'>" + data_report[i][v2] + "</td>";
        });
        tr.innerHTML = str_td;
        ofragment.appendChild(tr);
    }

    var str_td1 = '';
    var tr1 = document.createElement("tr");
    Object.keys(data_report[data_report.length - 2]).forEach(function (v2) {
        // str_td += "<td>" + data_report[q][v2] + "</td>";
        var a = data_report[data_report.length - 2][v2];
        var b = data_report[data_report.length - 1][v2];
        if (isRealNum(a) && isRealNum(b)) {
            str_td1 += "<td style='height: 36px;'>" + (parseFloat(a) + parseFloat(b)) + "</td>";
        } else {
            str_td1 += "<td style='height: 36px;'>" + a + "</td>";
        }

    });
    tr1.innerHTML = str_td1;
    ofragment.appendChild(tr1);


    report_table.appendChild(ofragment);
}

$(function () {
    $.post(Feng.ctxPath + "/tc/getList", function (data) {
        var txt = '<option value="">--请选择TC--</option>';
        for (var i = 0; i < data.length; i++) {
            txt += '<option value="' + data[i].tcName + '">' + data[i].tcName + '</option>';
        }
        document.getElementById('fieldTc').innerHTML = txt;
        //状态单选
        var seletefieldTc = $('#fieldTcValue').val();
        if (seletefieldTc != "") {
            $("#fieldTc option[value=" + seletefieldTc + "]").attr("selected", "selected");
        }
    }, "json");
    search_jztj();
    var oldStartTime = $("#startTime").val();
    var oldEndTime = $("#customerStatus").val();

    var oldFieldTc = $("#fieldTc").val();
    var oldCustomerStatus = $("#customerStatus").val();
    setInterval(
        function () {//定时执行方法内容
            if ($("#startTime").val() != oldStartTime || $("#endTime").val() != oldEndTime) {//如果时间发生变化，就触发查询事件
                search_jztj();
                //如果值发生变化之后，把最新的值赋值到老的变量里面
                oldStartTime = $("#startTime").val();
                oldEndTime = $("#endTime").val();
            }
            if ($("#fieldTc").val() != oldFieldTc) {
                search_jztj();
                //如果值发生变化之后，把最新的值赋值到老的变量里面
                oldFieldTc = $("#fieldTc").val();
            }
            if ($("#customerStatus").val() != oldCustomerStatus) {
                search_jztj();
                //如果值发生变化之后，把最新的值赋值到老的变量里面
                oldCustomerStatus = $("#customerStatus").val();
            }
        }, 500);
})